package com.x.sys.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.x.common.annotation.RequiredLog;
import com.x.common.bo.PageObject;
import com.x.common.exception.ServiceException;
import com.x.sys.dao.AdminDao;
import com.x.sys.entity.Admin;
import com.x.sys.service.AdminService;

import io.micrometer.core.instrument.util.StringUtils;

@Transactional(timeout = 60,
isolation = Isolation.READ_COMMITTED,
readOnly = false,
rollbackFor = Throwable.class,
propagation = Propagation.REQUIRED)
@Service
public class AdminServiceImpl implements AdminService {

	@Autowired
	private AdminDao adminDao;


	@Transactional(readOnly = true)
	//@Override
	public int validById(Integer id,Integer valid) {
		//1.合法性验证
		if(id==null||id<=0)
			throw new ServiceException("参数不合法,id="+id);
		if(valid!=1&&valid!=0)
			throw new ServiceException("参数不合法,valie="+valid);
		//2.执行禁用或启用操作(admin为后续登陆用户）
		int rows=adminDao.validById(id, valid,"zhaojun");
		//3.判定结果,并返回
		if(rows==0)
			throw new ServiceException("此记录可能已经不存在");
		return rows;
	}

	@Transactional(readOnly = true)//readOnly事务中不允许执行更新操作
	@RequiredLog(operation = "分页查询")
	//@Override
	public PageObject<Admin> findPageObjects(String username,Integer pageCurrent) {
		//1.参数校验（思考username允许为空吗？允许）
		if(pageCurrent==null||pageCurrent<1)
			throw new IllegalArgumentException("当前页码值不合法");	
		//1.根据，名字查询总记录数
		int rowCount=
				adminDao.getRowCount(username);
		if(rowCount==0) 
			throw new ServiceException("没有对应记录");
		//2.查询当前页记录
		//定义每页最多显示多少页
		int pageSize=3;
		int startIndex=(pageCurrent-1)*pageSize;
		List<Admin> records=
				adminDao.findPageObjects(username, startIndex, pageSize);
		//4.2)封装数据
		//5.返回封装结果。
		return new PageObject<>(records, rowCount, pageSize, pageCurrent);

	}
	/**添加删除业务的具体实现*/

	@RequiredLog(operation = "删除")
	public int deleteObjects(Integer... ids) {
		//1.判断参数合法性
		if(ids==null||ids.length==0)
			throw new IllegalArgumentException("请选择一个");
		//2.执行删除操作
		int rows;

		rows=adminDao.deleteObjects(ids);


		//4.对结果进行验证
		if(rows==0)
			throw new ServiceException("记录可能已经不存在");
		//5.返回结果
		return rows;
	}
/**添加*/
	@Override
	public void saveObject(Admin entity) {
		//1.参数有效性校验
		if(entity==null)
			throw new IllegalArgumentException("保存对象不能为空");
		if(StringUtils.isEmpty(entity.getA_name()))
			throw new IllegalArgumentException("角色名不允许为空");
		//将用户对应的角色信息写入导信息库
		adminDao.insertObject(entity);
	}
 /**修改*/
	/*
	 * @Override public List<Admin> findObject(Admin entity) { //1.合法性验证
	 * if(entity==null) throw new IllegalArgumentException("id的值不合法"); //2.执行更新
	 * List<Admin> result=adminDao.updateObject(entity); //3.验证结果并返回
	 * if(result==null) throw new ServiceException("此记录已经不存在"); return result; }
	 */

@Override
public Map<String, Object> findObjectById(Integer id) {
	//1参数校验
	
	//2.查询用户对用的角色信息
List<Admin> admin=adminDao.findObjectById(id);
if(admin==null)
	throw new ServiceException("此用户可能不存在");
	//3.封装查询结果并返回
	Map<String,Object> map=new HashMap<>();
	map.put("admin", admin);
	return map;
}

@Override
public void updateObject(Admin entity) {
	//参数校验
	if(entity==null)
		throw new IllegalArgumentException("保存对象不能为空");
	if(StringUtils.isEmpty(entity.getA_name()))
		throw new IllegalArgumentException("用户名不能空");
	//将用户信息更新到数据库
	adminDao.updateObject(entity);


}
}





